package com.xujianlun.cpms.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xujianlun.cpms.entity.sys.Menu;
import com.xujianlun.cpms.mapper.sys.MenuMapper;
import com.xujianlun.cpms.service.sys.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author xujianlun
 * @since 2022-11-24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> findAllMenus(String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isBlank(name)){
            queryWrapper.like("name",name);
        }

       // 新增的一个菜单排序
        queryWrapper.orderByAsc("sort");

        //查询所有数据
        List<Menu> menuList = list(queryWrapper);
        //找出pid为null的一级菜单
        List<Menu> parentMenus = menuList.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //找出一级菜单的子菜单
        for (Menu menu : parentMenus){
            //赛选数据中pid = 父级id的数据就是二级菜单
            menu.setChildren(menuList.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentMenus;
    }
}
